//
//  LS_ListDataBase.m
//  App_6.23
//
//  Created by 芦珊 on 16/7/1.
//  Copyright © 2016年 lushan. All rights reserved.
//

#import "LS_ListDataBase.h"
#import "FMDatabase.h"
#import "WH_Home.h"

@interface LS_ListDataBase ()

@property (nonatomic, retain)FMDatabase *db;

@end

@implementation LS_ListDataBase


+ (instancetype)shareDataBase{
    static LS_ListDataBase *dbList = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dbList = [[LS_ListDataBase alloc] init];
    });
    return dbList;
}

- (void)openDB{
    
    if (self.db != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    
    NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *fileName = [doc stringByAppendingPathComponent:@"List.sqlite"];
    NSLog(@"fileName = %@", fileName);
    self.db = [FMDatabase databaseWithPath:fileName];
    if ([self.db open]) {
        NSLog(@"数据库打开成功");
    }else {
        NSLog(@"数据库打开失败");
    }
    
}

- (void)createTable{
    
    BOOL result = [self.db executeUpdate:@"CREATE TABLE IF NOT EXISTS listTable (id INTEGER PRIMARY KEY AUTOINCREMENT, product_id TEXT, product_name TEXT, product_image TEXT, userName TEXT);"];
    if (result) {
        NSLog(@"创清单表成功");
    }else{
        NSLog(@"创清单表失败");
    }
    
}
- (void)insertHome:(NSString *)product_id image:(NSString *)product_image name:(NSString *)product_name user:(NSString *)userName{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO listTable(product_id, product_name, product_image, userName) VALUES('%@', '%@', '%@', '%@')", product_id, product_name,product_image,userName];
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
    
    
}
- (NSMutableArray *)selectHomeArray:(NSString *)userName{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM listTable WHERE userName = '%@'", userName];
    FMResultSet *resultSet = [self.db executeQuery:sql];
    while ([resultSet next]) {
        NSString *num = [resultSet objectForColumnName:@"product_id"];
        NSString *nameStr = [resultSet objectForColumnName:@"product_name"];
        NSString *img = [resultSet objectForColumnName:@"product_image"];
        WH_Home *home = [[WH_Home alloc] init];
        home.product_id = num;
        home.product_name = nameStr;
        home.product_image = img;
        [array addObject:home];
    }
    return array;
    
}
- (NSMutableArray *)selectHomeList:(NSString *)userName name:(NSString *)product_name{
    
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM listTable WHERE product_name = '%@' AND userName = '%@'", product_name,userName];
    FMResultSet *resultSet = [self.db executeQuery:sql];
    while ([resultSet next]) {
        NSString *num = [resultSet objectForColumnName:@"product_id"];
        NSString *nameStr = [resultSet objectForColumnName:@"product_name"];
        NSString *img = [resultSet objectForColumnName:@"product_image"];
        WH_Home *home = [[WH_Home alloc] init];
        home.product_id = num;
        home.product_name = nameStr;
        home.product_image = img;
        [array addObject:home];
    }
    return array;
    
}
- (void)deleteHome:(NSString *)product_name user:(NSString *)userName{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM listTable WHERE product_name = '%@' AND userName = '%@'", product_name,userName];
    BOOL result = [self.db executeUpdate:sql];
    if (result) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
}

@end
